/**
 * @properties={typeid:24,uuid:"BA84DD13-DE96-4EFE-B02E-047138DFBEFB"}
 * @AllowToRunInFind
 */
function buscar_pendientes() 
{	
	globals.gtotal_comprob = 0  
	// traer solamente los que tienen saldo y son debito
	// tambien traer creditos y anticipos
	if (globals.marca_primer_find == 0)
	{
		globals.marca_primer_find = 1
		controller.find()
		cliente_id 	= forms.rec_1.cliente_id
		saldo 		= '> 0'
		var found = controller.search()
		
		if (found > 0)
		{
			recalcula_total_comprob();
		}
		else
		{
			globals.DIALOGS.showWarningDialog('Aviso','No hay mov. de ctacte con saldo','OK')
		}
	}  
}

/**
 * @properties={typeid:24,uuid:"4E5DD690-0260-45B8-AA5C-CEFCBF6B1ABD"}
 */
function confirmar_seleccion() 
{
	//borrar  lo que tenga en rec_detalle_comprob de esa cabecera
	forms.rec_1.rec_cabecera_to_rec_detalle_comprob.deleteAllRecords()
	
	//recorre ctacte para los marcados = 1
	var found = databaseManager.getFoundSetCount(foundset)	
	if (found > 0)
	{
		globals.gtotal_comprob=0 
		for (var i = 1 ; i <= found ; i++)
		 {		 
			controller.setSelectedIndex(i)
			if (marcado_ctacte == 1)
			{
				//grabar lo que marcado en detalle_comprob
				forms.rec_1.rec_cabecera_to_rec_detalle_comprob.newRecord()
				forms.rec_1.rec_cabecera_to_rec_detalle_comprob.cod_mov			= cod_mov
				forms.rec_1.rec_cabecera_to_rec_detalle_comprob.fecha			= fecha_mov
				forms.rec_1.rec_cabecera_to_rec_detalle_comprob.importe			= saldo
				forms.rec_1.rec_cabecera_to_rec_detalle_comprob.imp_original	= saldo
				forms.rec_1.rec_cabecera_to_rec_detalle_comprob.nro_comprobante	= nro_factura
				forms.rec_1.rec_cabecera_to_rec_detalle_comprob.letra			= letra
				forms.rec_1.rec_cabecera_to_rec_detalle_comprob.sucursal		= sucursal
				forms.rec_1.rec_cabecera_to_rec_detalle_comprob.ctacte_cl_id	= ctacte_cl_id
			}
		 }
		recalcula_total_comprob()
		if (globals.gtotal_comprob < 0)
		{
			globals.DIALOGS.showWarningDialog('Aviso','El total de los comprobantes a imputar no puede ser negativo','OK')
		}
		else
		{
			// por final todo ok hacer close del form
			var $win = application.getWindow('ctacte_rec');
			$win.hide()
			$win.destroy()
		}
	}
	else
	{
		var $win = application.getWindow('ctacte_rec');
		$win.hide()
		$win.destroy()
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"3EF085BF-A0D5-4757-B48F-3862E1E831E4"}
 */
function marcar_todo(event) 
{
	globals.gtotal_comprob = 0;
	var $cant = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $cant ; i++)
	{
		controller.setSelectedIndex(i);
		var $rcd = foundset.getRecord(i)
		$rcd.marcado_ctacte = 1;
	}
	recalcula_total_comprob();
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"A15D5464-1ADA-4FAE-93AE-9A6AEA696555"}
 */
function desmarcar_todo(event) 
{
	globals.gtotal_comprob = 0;	
	var $cant = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $cant ; i++)
	{
		controller.setSelectedIndex(i);
		var $rcd = foundset.getRecord(i)
		$rcd.marcado_ctacte = 0;
	}
}

/**
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"C5CCECB2-2530-4412-8A67-8C915CC7137C"}
 */
function recalcula_total_comprob() 
{  
	globals.gtotal_comprob = 0;
	var $cant = databaseManager.getFoundSetCount(foundset)
	for (var j = 1 ; j <= $cant ; j++)
	{		 
		var $rcd = foundset.getRecord(j); 
		if ($rcd.marcado_ctacte == 1)
		{
			if($rcd.ctacte_cl_to_tab_mov_cl.d_h == 1)
				globals.gtotal_comprob += $rcd.saldo;
			else
				globals.gtotal_comprob -= $rcd.saldo;
		}
	}
	globals.grecibo_diferencia();
	return true
}
